﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AwaitAndAsyncStudy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //var webclint = new WebClient();
            //var result = webclint.DownloadString("https://gitee.com/");//下载字符串
            //MessageBox.Show(result);
            var webclint = new WebClient();
            var downloadThread = new Thread(() =>
            {
                var result = webclint.DownloadString("https://gitee.com/");//下载字符串
                    MessageBox.Show(result);
            });
            downloadThread.Start();
        }

        private async void DownloadBtn_Click(object sender, RoutedEventArgs e)
        {
            ////downloadBtn.IsEnabled = false;
            ////var webclint = new WebClient();
            //////var result = await webclint.DownloadStringTaskAsync("https://gitee.com");//下载字符串
            ////var downloadTask= webclint.DownloadStringTaskAsync("https://gitee.com");//下载字符串
            ////var result = await downloadTask;//下载字符串
            ////MessageBox.Show(result);
            ////downloadBtn.IsEnabled = true;
            //var task = SaveFile();
            //await task;
            var result =await SaveFileReturnString();
            MessageBox.Show(result);
        }
        public async Task<string> SaveFileReturnString()
        {
            var webclint = new WebClient();
            var result = await webclint.DownloadStringTaskAsync("https://gitee.com");
            await Task.Run(() =>
            {
                File.WriteAllText("D:/My/Learning/AwaitAndAsync/AwaitAndAsyncStudy/test.txt", result);
            });
            return "操作成功!";

        }
        public async Task SaveFile()
        {
            var webclint = new WebClient();
            var result = await webclint.DownloadStringTaskAsync("https://gitee.com");
            await Task.Run(() =>
            {
                File.WriteAllText("D:/My/Learning/AwaitAndAsync/AwaitAndAsyncStudy/test.txt", result);
            });
            
        }
    }
}
